<?php


namespace app\manage\controller;

use think\Db;
use think\Log;

class RequestLog extends Base
{
    public function _initialize()
    {
        parent::_initialize();
    }

    public function index()
    {
        // 查询条件start
        $clue_name = input('clue_name','');
        $mer_name = input('mer_name','');
        $start_time = input('start_time');
        $end_time = input('end_time');

        $map = [];
        if($clue_name != ''){
            $map['clue_name'] = ['like','%'.$clue_name.'%'];
        }
        if($mer_name != ''){
            $map1['mer_name'] = ['like','%'.$mer_name.'%'];
            $merchantId = model('Merchant')->where($map1)->column('id');
            if(!empty($merchantId)){
                $map['mer_id']=['in', $merchantId];
            }
        }
        if($start_time != null && $end_time != null){
            $start = strtotime($start_time);
            $end = strtotime($end_time);
            if($end > $start) $map['create_time'] = ['between',[$start,$end]];
        }
        // 查询条件end

        $systemLogs = model('RequestLog')->getInfosByMap($map, '', false);

        foreach ($systemLogs as $k=>$v){
            $merchant = model('Merchant')->field('mer_name')->where(['id'=>$v['mer_id']])->find();
            $mapping = model('Mapping')->field('api_url')->where(['mer_id'=>$v['mer_id']])->find();
            $systemLogs[$k]['mer_name'] = $merchant['mer_name'];
            $systemLogs[$k]['api_url'] = $mapping['api_url'];
        }

        return $this->fetch('', [
            'systemLogs' => $systemLogs,
            'start_time' => $start_time,
            'end_time' => $end_time,
            'clue_name' => $clue_name,
            'mer_name' => $mer_name,
        ]);
    }

    public function edit()
    {
        $id = input('id');
        if(!$id) return self::showReturnCode(1002,'id 错误');
        $info = model('RequestLog')->getInfoByMap(['id'=>$id]);
        return self::showJsonReturnCodeData('1001',$info,'');
    }
}